package cn.broadsense.thrid.api;

import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.*;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * xc 5300 接口
 *
 * @author pengxin
 * @date 2022-08-08 16:01
 */
@Slf4j
@Component
public class DaHuaApiRepository extends BaseRepository {


    private Boolean isTest = false;

    /**
     * 分页查询通道列表
     *
     * @param param
     * @return
     */
    public ChannelPageResult channelPage(ChannelPageParam param) {
        String dhToken = getCToken();
        param.setUrl(cUrl + DaHuaApiUrlConstant.CHANNEL_PAGE_POST);
        return DaHuaHttpUtils.postRest(dhToken, param);
    }

    /**
     * 查询通道详情
     *
     * @param param
     * @return
     */
    public ChannelResult channelDetail(ChannelDetailParam param) {
        String dhToken = getCToken();
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.CHANNEL_DETAILS_GET, param.getChannelCode()));
        return DaHuaHttpUtils.getRest(dhToken, param);
    }

    /**
     * 视频播放接口
     *
     * @param videoPlayRequest
     * @return
     */
    public VideoPlayResult videoPlay(VideoPlayParam videoPlayRequest) {
        if (isTest) {
            VideoPlayResult playResult = new VideoPlayResult();
            playResult.setUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
            return playResult;
        }

        String dhToken = getToken();
        videoPlayRequest.setUrl(url + DaHuaApiUrlConstant.VIDEO_PLAY_POST);
        try {
            return DaHuaHttpUtils.postRest(dhToken, videoPlayRequest);
        } catch (Exception e) {
            log.error("获取失败失败：{}", e.getMessage());
        }
        return null;
    }

    /**
     * 设备变焦
     *
     * @param param
     */
    public void videoCloudFocus(VideoCloudFocusParam param) {
        String dhToken = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.VIDEO_CLOUD_FOCUS_POST);
        DaHuaHttpUtils.postRest(dhToken, param);
    }

    /**
     * 设备云台控制
     *
     * @param param
     */
    public void videoCloudTurn(VideoCloudTurnParam param) {
        String dhToken = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.VIDEO_CLOUD_TURN_POST);
        DaHuaHttpUtils.postRest(dhToken, param);
    }


    /**
     * 点位能力绑定列表
     *
     * @param param
     * @return
     */
    public AbilityListResult ability(AbilityListParam param) {
        String dhToken = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.ABILITY_LIST);
        return DaHuaHttpUtils.postRest(dhToken, param);
    }


    /**
     * 点位算法规则
     *
     * @param param
     * @return
     */
    public AbilityRuleResult abilityRule(AbilityRuleParam param) {
        param.setUrl(url + DaHuaApiUrlConstant.ABILITY_RULE);
        return DaHuaHttpUtils.postRest(getToken(), param);
    }

    /**
     * 获取告警订阅中心列表页查询信息
     *
     * @param param
     * @return
     */
    public AlarmSubscribePageResult alarmSubscribePage(AlarmSubscribePageParam param) {
        String dhToken = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.ALARM_SUBSCRIBE_PAGE);
        return DaHuaHttpUtils.postRest(dhToken, param);
    }


    /**
     * 订阅（批量）、暂停（批量）
     *
     * @param param
     */
    public void subscribeAbility(SubscribeAbilityParam param) {
        String dhToken = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.SUBSCRIBE_ABILITY);
        DaHuaHttpUtils.postRest(dhToken, param);
    }

    public JSONObject play(String channelId) {
        String dhToken = getToken();
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return JSONObject.class;
            }
        };
        param.setUrl(url + String.format(DaHuaApiUrlConstant.VIDEO_PLAY, channelId, 0));
        return DaHuaHttpUtils.getRest(dhToken, param);

    }

    /**
     * 设备组织树
     *
     * @param param
     * @return
     */
    public JSONObject deviceTree(DeviceTreeParam param) {
        String dhToken = getCToken();
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(param);
        stringObjectMap.put("resultClass", null);
        stringObjectMap.put("ignoreList", null);
        stringObjectMap.put("url", null);
        String query = MapUtil.joinIgnoreNull(stringObjectMap, "&", "=");
        param.setUrl(cUrl + DaHuaApiUrlConstant.DEVICE_TREE + "?" + query);
        return DaHuaHttpUtils.getRest(dhToken, param);
    }

    /**
     * 跳转5300 页面
     *
     * @param redirectParam
     * @return
     */
    public String compositeLogin(LoginRedirectParam redirectParam) {
        redirectParam.setUrl(url + DaHuaApiUrlConstant.COMPOSITE_LOGIN_REDIRECT);
        if (StrUtil.isBlank(redirectParam.getRedirectParam().getToken())) {
            String token = getToken();
            redirectParam.getRedirectParam().setToken(token);
        }
        JSONObject result = DaHuaHttpUtils.postRest(redirectParam.getRedirectParam().getToken(), redirectParam);
        return result.getString("data");
    }
}
